<template>
    <div>
        <a href="/DentView">演示导入导出的网页</a><br>
        <a href='/AddView'>新增</a><br>
        <input type="button" value="批量删除" @click="Del">
        <table>
            <thead>
                <tr>
                    <td><input type="checkbox" @change="select" v-model="selectall"></td>
                    <td>姓名</td>
                    <td>数量</td>
                    <td>价钱</td>
                    <td>图片</td>
                    <td>上下架</td>
                </tr>
            </thead>
            <tbody>
                <tr v-for="m in data">
                    <td><input v-model="idlist" type="checkbox" :value="m.id"></td>
                    <td>{{ m. name}}</td>
                    <td>{{ m. num}}</td>
                    <td>{{ m. price}}</td>
                    <td> <img :src="'https://localhost:7087'+m.img" style="width: 80px;height: 80px;" alt=""></td>
                    <td>{{ m. shen?"上架":"下架"}}</td>
                </tr>
            </tbody>
        </table>
        <input type="button" value="首页" @click="Fent(1)">
        <input type="button" value="上一页" @click="Fent(2)">
        <input type="button" value="下一页" @click="Fent(3)">
        <input type="button" value="尾页" @click="Fent(4)">
        一共{{ page.count }}条，一共{{ page.pcount }}页，每页{{ qag.parsize }}条，当前的{{ qag.pardex }}页
        <select @change="setsize" v-model="qag.parsize">
            <option>2</option>
            <option>4</option>
            <option>6</option>
        </select>
        <input type="text" v-model="qag.pardex">
        <input type="button" value="跳转" @click="setdex">
    </div>
</template>

<script setup lang="ts">
import {ref ,onMounted} from 'vue';
import axios  from 'axios';

const idlist=ref([0]);

const selectall=ref(false)

const select=()=>{
    if(selectall.value){
        for(var i=0;i<data.value.length;i++){
            idlist.value.push(data.value[i].id);
        }
    }
    else{
        idlist.value=[0]
    }
}

const Del=()=>{
    if(confirm("确认批量删除吗?")){
        return;
    }
    axios.delete("https://localhost:7087/api/User/Delete",{params:{id:idlist.value.toString()}}).then(res=>{
        if(res.data>0){
            alert('批量删除成功')
            Show();
        }
        else{
            alert('失败')
        }

    })
}



onMounted(() => {
   Show()  
})

const setsize=()=>{
    qag.value.pardex=1;
    Show()
}

const setdex=()=>{
    if(qag.value.pardex>0&&qag.value.pardex<=page.value.pcount){
        Show()
    }
}


const Fent=(val:any)=>{
    if(val==1){
        qag.value.pardex=1;
        Show()
    }
    if(val==2){
        if(qag.value.pardex>1){
            qag.value.pardex-=1;
            Show()
        }
    }
    if(val==3){
        if(qag.value.pardex<page.value.pcount){
            qag.value.pardex+=1;
            Show()
        }
    }
    if(val==4){
        qag.value.pardex=page.value.pcount;
        Show()
    }
}

const data=ref([{
    "id": 0,
  "name": "",
  "num": "",
  "price": 0,
  "img": "",
  "shen": true
}])

const qag=ref({
    pardex:1,
    parsize:2
})

const page=ref({
    count:0,
    pcount:0
})

const Show=()=>{
    axios.get("https://localhost:7087/api/User/Show",{params:qag.value}).then(res=>{
        data.value=res.data.data;
        page.value.count=res.data.count;
        page.value.pcount=Math.ceil(res.data.count/qag.value.parsize);
    })
}



</script>

<style scoped>

</style>